package com.xihua.sx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.xihua.sx.mapper.CarInfoMapper;
import com.xihua.sx.pojo.CarInfo;
import com.xihua.sx.pojo.PageData;
import com.xihua.sx.service.CarInfoService;
import com.xihua.sx.utils.ResponseMessage;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CarInfoServiceImpl implements CarInfoService {
    @Resource
    private CarInfoMapper carInfoMapper;
    @Override
    public ResponseMessage carInfoById(Integer carId) {
        //mybatisplus用来封装sql语句条件的对象
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id",carId);
        CarInfo carInfo = carInfoMapper.selectOne(queryWrapper);
        return new ResponseMessage(200,"carInfoById",carInfo);
    }
    //模糊查询车牌号
    @Override
    public ResponseMessage carInfoByCode(CarInfo carInfo) {
        QueryWrapper<CarInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(carInfo.getCarBrand()!=null,"car_brand",carInfo.getCarBrand());
        queryWrapper.like(carInfo.getCarCode()!=null,"car_code",carInfo.getCarCode());
        List<CarInfo> carInfoList = carInfoMapper.selectList(queryWrapper);
        System.out.println(carInfo);
        return new ResponseMessage(200,"carCode List",carInfoList);
    }
    //分页查询
    @Override
    public ResponseMessage carInfoAll(PageData pageData) {
        //通过前端传过来的pageData对象中的数据，创建分页对象page
        Page<CarInfo> page = new Page<>(pageData.getPage(), pageData.getSize());
        //执行查询
        Page<CarInfo> carInfoPage = carInfoMapper.carList(page);

        return new ResponseMessage(200,"Car list",carInfoPage);
    }

    @Override
    public ResponseMessage carInfoDele(Integer carId) {
        int carInfoList = carInfoMapper.deleteById(carId);
        return new ResponseMessage(200,"删除成功！",carInfoList);
    }

    @Override
    public ResponseMessage carInfoInsert(CarInfo carInfo) {
        int insert = carInfoMapper.insert(carInfo);
        return new ResponseMessage(200,"插入成功",insert);
    }

    @Override
    public ResponseMessage carInfoUpdate(CarInfo carInfo) {
        int update = carInfoMapper.updateById(carInfo);
        return new ResponseMessage(200,"修改成功",update);
    }


}
